import torch
import torchvision
from PIL import Image
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential

image_path = "./images/OIP-C.jpg"
image = Image.open(image_path)

transform = torchvision.transforms.Compose([torchvision.transforms.Resize((32, 32)),
                                        torchvision.transforms.ToTensor()])
image = transform(image)

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

class module(nn.Module):
    def __init__(self):
        super(module, self).__init__()
        self.model1 = Sequential(
            Conv2d(3, 32, 5, 1, 2),
            MaxPool2d(2),
            Conv2d(32, 32, 5, 1, 2),
            MaxPool2d(2),
            Conv2d(32, 64, 5, 1, 2),
            MaxPool2d(2),
            Flatten(),
            Linear(1024, 64),
            Linear(64, 10)
        )

    def forward(self, input):
        x = self.model1(input)
        return x

# 导入模型方法1
model = torch.load('./train_module/Module_0_save1.pth')
# 导入模型方法2
# model = module()
# model.load_state_dict(torch.load('./train_module/Module_0.pth'))
model.to(device)
print(model)
image = torch.reshape(image, (1, 3, 32, 32))
image = image.to(device)
model.eval()
with torch.no_grad():
    output = model(image)
print(output)
print(output.argmax(1))